package com.cst.zju.anime.utils;

import java.util.List;


/** 
* @Description: Page类
* @author BRUCE
* @date 2014年11月5日 下午8:05:07 
* 
* @param <T> 
*/
public class Page<T> {
	public static final int DEFAULT_RECORD_SIZE = 10;
	public static final int DEFAULT_PAGE_SIZE = 10;
	
	// 分页查询开始记录位置
	private int begin;
	// 分页查看下结束位置
	private int end;
	// 开始页码
	private int beginPageNum;
	// 结束页码
	private int endPageNum;
	// 页码长度
	private int pageSize;
	// 每页显示记录数
	private int length;
	// 查询结果总记录数
	private int totalRecords;
	// 当前页码
	private int currentPage;
	// 总共页数
	private int totalPage;
	// 对应的当前页记录
	private List<T> resultList;
	
	
	public Page() {
		this.currentPage = 1;
        this.length = DEFAULT_RECORD_SIZE;
        this.pageSize = DEFAULT_PAGE_SIZE;
        this.begin = (getCurrentPage() - 1) * getLength();
        this.end = getCurrentPage() * getLength();
        
        if((this.currentPage-Math.ceil(this.pageSize/2)) < 1){
        	this.beginPageNum = 1;
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }else{
        	this.beginPageNum = this.currentPage-(int)Math.ceil(this.pageSize/2);
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }
	}

	/**
	 * 构造函数
	 * 
	 * @param begin
	 * @param length
	 */
//	public Page(int begin, int length) {
//		this.begin = begin;
//		this.length = length;
//		this.end = this.begin + this.length;
//		this.currentPage = (int) Math.floor((this.begin * 1.0d) / this.length) + 1;
//	}



	/**
	 * @param begin
	 * @param length
	 * @param totalRecords
	 */
//	public Page(int begin, int length, int totalRecords) {
//		this(begin, length);
//		this.totalRecords = totalRecords;
//	}

	  /**
     * 设置页数，自动计算数据范围.
     * 
     * @param currentPage
     */
    public Page(int currentPage) {        
    	this.currentPage = currentPage;
    	this.length = DEFAULT_RECORD_SIZE;
        this.pageSize = DEFAULT_PAGE_SIZE;
        
    	currentPage = currentPage > 0 ? currentPage : 1;  
        this.begin = this.length * (currentPage - 1);        
        this.end = this.length * currentPage;
        if((this.currentPage-Math.ceil(this.pageSize/2)) < 1){
        	this.beginPageNum = 1;
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }else{
        	this.beginPageNum = this.currentPage-(int)Math.ceil(this.pageSize/2);
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }
       
    }

	/**
	 * 设置当前页和长度，自动计算数据范围.
	 * @param currentPage
	 * @param length
	 */
	public Page(int currentPage,int length) {
		this.length = length;
		this.currentPage = currentPage;
		this.pageSize = DEFAULT_PAGE_SIZE;
		currentPage = currentPage > 0 ? currentPage : 1;
		this.begin = this.length * (currentPage - 1);
		this.end = this.length * currentPage;
		
		if((this.currentPage-Math.ceil(this.pageSize/2)) < 1){
        	this.beginPageNum = 1;
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }else{
        	this.beginPageNum = this.currentPage-(int)Math.ceil(this.pageSize/2);
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }
	}
	
	/**
	 * 设置当前页、长度和页码长度，自动计算数据范围.
	 * @param currentPage
	 * @param length
	 */
	public Page(int currentPage,int length, int pageSize) {
		this.length = length;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		currentPage = currentPage > 0 ? currentPage : 1;
		this.begin = this.length * (currentPage - 1);
		this.end = this.length * currentPage;
		
		if((this.currentPage-Math.ceil(this.pageSize/2)) < 1){
        	this.beginPageNum = 1;
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }else{
        	this.beginPageNum = this.currentPage-(int)Math.ceil(this.pageSize/2);
        	this.endPageNum = this.beginPageNum + this.pageSize - 1;
        }
	}

	/**
	 * @return the begin
	 */
	public int getBegin() {
		return begin;
	}

	/**
	 * @return the end
	 */
	public int getEnd() {
		return end;
	}

	/**
	 * @param end
	 *            the end to set
	 */
	public void setEnd(int end) {
		this.end = end;
	}

	/**
	 * @param begin
	 *            the begin to set
	 */
	public void setBegin(int begin) {
		this.begin = begin;
		if (this.length != 0) {
			this.currentPage = (int) Math.floor((this.begin * 1.0d) / this.length) + 1;
		}
	}

	/**
	 * @return the length
	 */
	public int getLength() {
		return length;
	}

	/**
	 * @param length
	 *            the length to set
	 */
	public void setLength(int length) {
		this.length = length;
		if (this.begin != 0) {
			this.currentPage = (int) Math.floor((this.begin * 1.0d) / this.length) + 1;
		}
	}

	/**
	 * @return 开始页码
	 */	
	public int getBeginPageNum() {
		return beginPageNum;
	}

	/**
	 * @param beginPageNum 
	 * 设置开始页码
	 */
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	
	/**
	 * @return 结束页码
	 */	
	public int getEndPageNum() {
		return endPageNum;
	}

	/**
	 * @param endPageNum 
	 * 设置结束页码
	 */
	public void setEndPageNum(int endPageNum) {
		this.endPageNum = endPageNum;
	}

	/**
	 * @return 页码长度
	 */	
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize 
	 * 设置页码长度
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the totalRecords
	 */
	public int getTotalRecords() {
		return totalRecords;
	}

	/**
	 * @param totalRecords
	 *            the totalRecords to set
	 */
	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
		this.totalPage = (int) Math.floor((this.totalRecords * 1.0d) / this.length);
		if (this.totalRecords % this.length != 0) {
			this.totalPage++;
		}
		if(this.endPageNum > this.totalPage){
        	this.endPageNum = this.totalPage;
        	this.beginPageNum = (this.endPageNum - this.pageSize)> 0 ? (this.endPageNum - this.pageSize): 1;
        }
	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	    this.currentPage = currentPage;
	    currentPage = currentPage > 0 ? currentPage : 1;
        this.begin = this.length * (currentPage - 1);
        this.end = this.length * currentPage;
	}

	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		if (totalPage == 0) {
			return 1;
		}
		return totalPage;
	}

	/**
	 * @param totalPage
	 *            the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	 public List<T> getResultList() {
		return resultList;
	}

	public void setResultList(List<T> resultList) {
		this.resultList = resultList;
	}

	@Override
	 public String toString() {        
		 final StringBuilder builder = new StringBuilder("begin=").append(begin).append(", end=")
             .append(end).append(", length=").append(length).append(", totalRecords=").append(
            		 totalRecords).append(", currentPage=").append(currentPage).append(", totalPage=")
             .append(totalPage);
		 return builder.toString();
 }
}

